<script lang="ts" setup>
import { ref, computed, onMounted, ComputedRef } from "vue"
import { ElMessage } from "element-plus"
import { Bell } from "@element-plus/icons-vue"
import NotifyList from "./NotifyList.vue"

import { type ListItem } from "./types"
import { createAuthenticatedSSE } from "@/hooks/useSSE"
import { getToken } from "@/utils/cache/cookies"
import { reReadMessage } from "@/api/user/index"

type TabName = "系统消息" | "用户消息" | "企业消息"

interface DataItem {
  name: TabName
  type: "primary" | "success" | "warning" | "danger" | "info"
  list: ComputedRef<ListItem[]>
}

const token = getToken()

/** 角标当前值 */
const badgeValue = computed(() => {
  return data.value.reduce((sum, item) => {
    return sum + item.list.filter((li) => li.msgStatus === 0).length
  }, 0)
})
/** 角标最大值 */
const badgeMax = 99
/** 面板宽度 */
const popoverWidth = 350
/** 当前 Tab */
const activeName = ref<TabName>("系统消息")
/** 创建SSE */
const { data: receiveData, conenct } = createAuthenticatedSSE<ListItem>("/api/v1/message/sse/my", {
  Authorization: `Bearer ${token}`
})
/** 所有数据 */
const data = ref<DataItem[]>([
  // 用户消息
  {
    name: "用户消息",
    type: "info",
    list: computed(() => {
      return receiveData.value?.filter((item) => item.msgType === 1) ?? []
    })
  },
  // 企业消息
  {
    name: "企业消息",
    type: "success",
    list: computed(() => {
      return receiveData.value?.filter((item) => item.msgType === 2) ?? []
    })
  },
  // 系统消息
  {
    name: "系统消息",
    type: "primary",
    list: computed(() => {
      return receiveData.value?.filter((item) => item.msgType === 0) ?? []
    })
  }
])

const handleReReadMsg = () => {
  reReadMessage({ messageIds: [] })
}

const handleHistory = () => {
  ElMessage.success(`跳转到${activeName.value}历史页面`)
}

onMounted(() => {
  conenct()
})
</script>

<template>
  <div class="notify">
    <el-popover placement="bottom" :width="popoverWidth" trigger="click">
      <template #reference>
        <el-badge :value="badgeValue" :max="badgeMax" :hidden="badgeValue === 0">
          <el-tooltip effect="dark" content="消息通知" placement="bottom">
            <el-icon :size="20">
              <Bell />
            </el-icon>
          </el-tooltip>
        </el-badge>
      </template>
      <template #default>
        <el-tabs v-model="activeName" class="demo-tabs" stretch>
          <el-tab-pane v-for="(item, index) in data" :name="item.name" :key="index">
            <template #label>
              {{ item.name }}
              <el-badge
                :value="item.list.filter((li) => li.msgStatus === 0).length"
                :max="badgeMax"
                :type="item.type"
              />
            </template>
            <el-scrollbar height="400px">
              <NotifyList :list="item.list" />
            </el-scrollbar>
          </el-tab-pane>
        </el-tabs>
        <div class="notify-history">
          <el-button text plain @click="handleReReadMsg">全部已读</el-button>
          <el-button link @click="handleHistory">查看{{ activeName }}历史</el-button>
        </div>
      </template>
    </el-popover>
  </div>
</template>

<style lang="scss" scoped>
.notify {
  margin-right: 10px;
}

.notify-history {
  text-align: center;
  padding-top: 12px;
  border-top: 1px solid var(--el-border-color);
}
</style>
